import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import { VitePWA } from 'vite-plugin-pwa'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    VitePWA({
      workbox: {
        globPatterns: ['**/*.{js,css,html,png}'],
      },
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  server: {
    port: 9527, // 端口号,
    cors: true, // 允许跨域
    proxy: {
      '/dev-api': {
        target: 'https://api.coze.cn', // 代理的目标地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/dev-api/, ''),
      },
    },
  },
  build: {
    target: 'esnext', // 使用现代ES特性
    cssTarget: 'chrome85', // 针对新浏览器优化CSS
    modulePreload: {
      polyfill: false, // 移除过时polyfill
    },
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true, // 删除console语句
        drop_debugger: true, // 删除debugger语句
      },
    },
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            return 'vendor' // 第三方依赖单独分包
          }
        },
      },
    },
  },
})
